// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using NSQL.Sharpen;

namespace NSQL.Expressions.Functions
{
    public class FormatFunction : BaseExpression, IFormatFunction
    {
        public bool IsDateFormat { get; private set; }
        public IConstantStringExpression Format { get; }
        public IExpressions Items { get; }

        public FormatFunction(IDateTimeExpression expression, IConstantStringExpression format)
        {
            IsDateFormat = true;
            Items = new ExpressionCollection {expression};
            Format = format;
        }

        public FormatFunction(IConstantStringExpression format, params IExpression[] expressions)
        {
            IsDateFormat = false;
            Items = new ExpressionCollection(expressions);
            Format = format;
        }

        public FormatFunction(IConstantStringExpression format, IEnumerable<IExpression> expressions)
        {
            IsDateFormat = false;
            Items = new ExpressionCollection(expressions);
            Format = format;
        }

        public FormatFunction(IConstantStringExpression format, IExpressions expressions)
        {
            IsDateFormat = false;
            Items = expressions;
            Format = format;
        }

        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);

        public override void Accept(IExpressionVisitor visitor) => visitor.Visit(this);

        public override bool IsValid => Format != null && Items != null && Items.IsValid;

        protected override IExpression DeepCopy()
        {
            return new FormatFunction(Format.DeepClone(), Items.DeepClone())
            {
                IsDateFormat = IsDateFormat,
                Tag = Tag
            };
        }
    }
}